﻿/**
* @file
* @brief initialization file read and write API
* @author Deng Yangjun
* @date 2007-12-9
* @version 0.2
*  (C)2007 Midapex
* This program is free software; you can redistribute it and/or modify it
*  under the terms of the GNU Library General Public License as published
*  by the Free Software Foundation; either version 2 of the License,
*  or (at your option) any later version.
*/
#ifndef INI_FILE_CPP_H_
#define INI_FILE_CPP_H_

#include <string>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#include <sys/sysinfo.h>
#include <stdio.h>
#include <vector>
#include <list>
#ifdef WIN32
	#define separator "\\"
#else
	#include <dirent.h>
	#include <sys/stat.h>
	#include <sys/stat.h>
	#include <sys/types.h> 
	#include <unistd.h>
	#include <sys/ioctl.h>
	//硬盘参数头文件， hd_driveid结构声明头文件
	#include <linux/hdreg.h>
	//文件控制头文件
	#include <sys/fcntl.h>
	#define separator "/"
#endif

#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

using namespace std;

namespace common
{
	class IniFile
	{
	public:
		IniFile(const string & fileName);
	public:
		virtual ~IniFile(void);
		const string & getFileName() const;
		const string &getSection() const;
		void setSection(const string &section);
		bool write(const string &key, const string & value) const;
		bool write(const string &key, int value) const;
		string readStr(const string &key, const string &default_value) const;
		int readInt(const string &key, int default_value) const;
	public:
		static int read_profile_string(const char *section, const char *key, char *value,
			int size, const char *default_value, const char *file);
		static int read_profile_int(const char *section, const char *key, int default_value,
			const char *file);
		static int write_profile_string(const char *section, const char *key,
			const char *value, const char *file);

	private:
		static int load_ini_file(const char *file, char *buf, int *file_size);
		static int newline(char c);
		static int end_of_string(char c);
		static int left_barce(char c);
		static int right_brace(char c);
		static int parse_file(const char *section, const char *key, const char *buf, int *sec_s, int *sec_e,
			int *key_s, int *key_e, int *value_s, int *value_e);

	private:
		std::string m_fileName;
		std::string m_section;
	};
    std::string itos(int n);
    std::string ltos(unsigned long long t);
    bool myatoi(std::string strInput,int &iNumber);
    unsigned long long stol(std::string t);
    void currentTimeMillisec(string &t);
	std::string GetProgramDir();
	std::string GetLocalDateTime();
    std::string utcToLocalTime(std::string strTime);
	bool DirectExists(const std::string& dirName_in);
	bool CreateDirect(std::string & dirName);
    int DeleteFile(const std::string strFilePaht);
	string StampToTime(int stampTime);
#ifndef WIN32
	std::string GetSystemBootTime();
	string GetFirstDiskSN();
#endif
	vector<string> split(const string  & str,const string &pattern);
	string trim(string s);
    std::string getYearMonthFromTime(time_t theTime);
    std::string getCurrentTime();
    bool ExecuteCMD(const char *cmd, char *result);
    bool AllisNum(string str);
    std::string GetSystemPoweronTime();
    std::string GetHardDiskSerial();
    int readFileList(std::string strFilePath,std::list<std::string> &fileName);
    void string_replaceAll(std::string &str, const std::string &src, const std::string &dst);
    bool CheckMd5Value(const char* p, const int iLen, std::string strMd5);
    int  CheckMd5Value(std::string strFilePath, std::string strCheckMd5Value);
    int mySystem(std::string command ,std::string &strRetutnValue);
    int FindFile(const char *file,int iLen);
    bool CheckMd5AndWriteFile(char* pContent, int iLen, std::string strMd5Valude);
    bool SetSystemTime(long long int nMilliseconds,int iSyncMaxTimeDiff,int iSyncMinTimeDiff);
    bool StrToToupperStr(std::string &strInput);
    bool isIPAddressValid(const char* pszIPAddr);
    bool checkGuidFormat(std::string strGUID);
    bool checkMacAddr(const string &str);
    bool isValidIP(string ip);
    bool isValidIPsegment(string ip);

    bool isInRange(string ip, string cidrIp, int maskBit);
    vector<int> splitwxl(const string &str,const string &pattern);
 }

#endif

